package com.freshfresh.activity;

import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.Toast;

import com.freshfresh.activity.classification.ClassificationActivity;
import com.freshfresh.activity.home.HomeActivity;
import com.freshfresh.activity.selfcenter.SelfCenterActivity;
import com.freshfresh.activity.shoppingcar.ShoppingCarActivity;
import com.hangyjx.widget.dialog.CustomDialog;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MainActivity extends TabActivity implements
		OnCheckedChangeListener {
	private Context context = null;
	long firstTime;
	private static final String tabHome = "TAB_HOME";
	private static final String tabAround = "TAG_AROUND";
	private static final String tabRecommend = "TAB_RECOMMEND";
	private static final String tabComplaints = "TAB_COMPLAINTS";
	private static final String tabPrice = "TAB_PRICE";
	private Intent homeIntent, aroundIntent, recommedIntent, complaintsIntent;

	private TabHost mTabHost;

	private RadioButton rbHome, rbAround, rbRecommend, rbComplaints;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.maintabs_activity);
		initView();
		initData();

	}

	private void initView() {
		mTabHost = getTabHost();

		homeIntent = new Intent(this, HomeActivity.class);
		aroundIntent = new Intent(this, ClassificationActivity.class);
		recommedIntent = new Intent(this, ShoppingCarActivity.class);
		complaintsIntent = new Intent(this, SelfCenterActivity.class);

		rbHome = ((RadioButton) findViewById(R.id.radio_home));
		rbAround = ((RadioButton) findViewById(R.id.radio_around));
		rbRecommend = ((RadioButton) findViewById(R.id.radio_recommend));
		rbComplaints = ((RadioButton) findViewById(R.id.radio_complaints));

		context = MainActivity.this;

	}

	private void initData() {
		rbHome.setOnCheckedChangeListener(this);
		rbAround.setOnCheckedChangeListener(this);
		rbRecommend.setOnCheckedChangeListener(this);
		rbComplaints.setOnCheckedChangeListener(this);

		mTabHost.addTab(buildTabSpec(tabHome, homeIntent));
		mTabHost.addTab(buildTabSpec(tabAround, aroundIntent));
		mTabHost.addTab(buildTabSpec(tabRecommend, recommedIntent));
		mTabHost.addTab(buildTabSpec(tabComplaints, complaintsIntent));

		rbHome.setChecked(true);
		mTabHost.setCurrentTabByTag(tabHome);

	}

	private TabHost.TabSpec buildTabSpec(String tag, Intent intent) {
		TabHost.TabSpec tabSpec = mTabHost.newTabSpec(tag);
		tabSpec.setContent(intent).setIndicator("");
		return tabSpec;
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radio_home:
				mTabHost.setCurrentTabByTag(tabHome);
				break;
			case R.id.radio_around:
				mTabHost.setCurrentTabByTag(tabAround);
				break;
			case R.id.radio_recommend:
				mTabHost.setCurrentTabByTag(tabRecommend);
				break;
			case R.id.radio_complaints:
				mTabHost.setCurrentTabByTag(tabComplaints);
				break;
			}
		}
	}

	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() != KeyEvent.ACTION_UP
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			// to do..
			long secondTime = System.currentTimeMillis();
			if (secondTime - firstTime > 800) {// 如果两次按键时间间隔大于800毫秒，则不退出
				Toast.makeText(context, "再按一次退出程序...", Toast.LENGTH_SHORT)
						.show();
				firstTime = secondTime;// 更新firstTime
				return true;
			} else {
				
//				ImageLoader.getInstance().clearMemoryCache();  
//				ImageLoader.getInstance().clearDiscCache();
				
				MainActivity.this.finish();
				System.exit(0);
			}
		}
		return super.dispatchKeyEvent(event);
	}

	// dialog
	public void operateDialog(Context context, String title, String message,
			String positiveButtonText, String negativeButtonText) {
		CustomDialog.Builder builder = new CustomDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		//确定按钮
		builder.setPositiveButton(positiveButtonText,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						SharedPreferences sahred = getSharedPreferences(
								"userinfo", MODE_PRIVATE);
						sahred.edit().clear().commit();
						Intent intent = new Intent("com.weixi.action1");
						sendBroadcast(intent);
						dialog.dismiss();
					}
				});
		builder.setNegativeButton(negativeButtonText,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.createB2().show();
	}
}
